home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_136 / bison / files.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  276 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  25. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include "files.h"
  30. #include "new.h"
  31. #include "gram.h"
  32.  
  33. FILE *finput = NULL;
  34. FILE *foutput = NULL;
  35. FILE *fdefines = NULL;
  36. FILE *ftable = NULL;
  37. FILE *fattrs = NULL;
  38. FILE *fguard = NULL;
  39. FILE *faction = NULL;
  40. FILE *fparser = NULL;
  41. /* FILE *fparser1 = NULL; JF we don't need this anymore */
  42.  
  43. char *infile;
  44. char *outfile;
  45. char *defsfile;
  46. char *tabfile;
  47. char *attrsfile;
  48. char *guardfile;
  49. char *actfile;
  50. char *tmpattrsfile;
  51. char *tmptabfile;
  52. /* JF nobody really uses these */
  53. /* char *pfile;
  54. char *pfile1; */
  55.  
  56. #ifndef AMIGA
  57. char    *mktemp();      /* So the compiler won't complain */
  58. #endif
  59. FILE    *tryopen();     /* This might be a good idea */
  60.  
  61. extern int verboseflag;
  62. extern int definesflag;
  63. int fixed_outfiles = 0;
  64.  
  65.  
  66. char*
  67. stringappend(string1, end1, string2)
  68. char *string1;
  69. int end1;
  70. char *string2;
  71. {
  72.   register char *ostring;
  73.   register char *cp, *cp1;
  74.   register int i;
  75.  
  76.   cp = string2;  i = 0;
  77.   while (*cp++) i++;
  78.  
  79.   ostring = NEW2(i+end1+1, char);
  80.  
  81.   cp = ostring;
  82.   cp1 = string1;
  83.   for (i = 0; i < end1; i++)
  84.     *cp++ = *cp1++;
  85.  
  86.   cp1 = string2;
  87.   while (*cp++ = *cp1++) ;
  88.  
  89.   return ostring;
  90. }
  91.  
  92.  
  93. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  94.    the output files, and opens the tmp files and the parser */
  95. openfiles()
  96. {
  97.   char *name_base;
  98.   register int i;
  99.   register char *cp;
  100.   char *filename;
  101.  
  102. #ifdef VMS
  103.   char *tmp_base = "sys$scratch:b_";
  104. #else
  105. # ifdef AMIGA
  106.   char *tmp_base = "t:b.";
  107. # else
  108.   char *tmp_base = "/tmp/b.";
  109. # end if
  110. #endif
  111.   int tmp_len = strlen (tmp_base);
  112.  
  113.   name_base = fixed_outfiles ? "y.y" : infile;
  114.  
  115.   cp = name_base;
  116.   i = 0;
  117.   while (*cp++) i++;
  118.  
  119.   cp -= 2;
  120.  
  121.   if (*cp-- == 'y' && *cp == '.')
  122.     i -= 2;
  123.  
  124.  
  125.   finput = tryopen(infile, "r");
  126.  
  127.   filename = (char *) getenv ("BISON_SIMPLE");
  128.   fparser = tryopen(filename ? filename : PFILE, "r");
  129.  
  130.   if (verboseflag)
  131.     {
  132.       outfile = stringappend(name_base, i, ".output");
  133.       foutput = tryopen(outfile, "w");
  134.     }
  135.  
  136.   if (definesflag)
  137.     {
  138. #ifdef VMS
  139.       defsfile = stringappend(name_base, i, "_tab.h");
  140. #else
  141.       defsfile = stringappend(name_base, i, ".tab.h");
  142. #endif
  143.       fdefines = tryopen(defsfile, "w");
  144.     }
  145.  
  146. #ifndef AMIGA
  147.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  148. #else
  149.   actfile = stringappend(tmp_base, tmp_len, "act.tmp");
  150. #endif
  151.   faction = tryopen(actfile, "w+");
  152.   unlink(actfile);
  153.  
  154. #ifndef AMIGA
  155.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  156. #else
  157.   tmpattrsfile = stringappend(tmp_base, tmp_len, "attrs.tmp");
  158. #endif
  159.   fattrs = tryopen(tmpattrsfile,"w+");
  160.   unlink(tmpattrsfile);
  161.  
  162. #ifndef AMIGA
  163.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  164. #else
  165.   tmptabfile = stringappend(tmp_base, tmp_len, "tab.tmp");
  166. #endif
  167.   ftable = tryopen(tmptabfile, "w+");
  168.   unlink(tmptabfile);
  169.  
  170.         /* These are opened by `done' or `open_extra_files', if at all */
  171. #ifdef VMS
  172.   tabfile = stringappend(name_base, i, "_tab.c");
  173.   attrsfile = stringappend(name_base, i, "_stype.h");
  174.   guardfile = stringappend(name_base, i, "_guard.c");
  175. #else
  176.   tabfile = stringappend(name_base, i, ".tab.c");
  177.   attrsfile = stringappend(name_base, i, ".stype.h");
  178.   guardfile = stringappend(name_base, i, ".guard.c");
  179. #endif
  180. }
  181.  
  182.  
  183.  
  184. /* open the output files needed only for the semantic parser.
  185. This is done when %semantic_parser is seen in the declarations section.  */
  186. open_extra_files()
  187. {
  188.   FILE *ftmp;
  189.   int c;
  190.   char *filename;
  191.                 /* JF change open parser file */
  192.   fclose(fparser);
  193.   filename = (char *) getenv ("BISON_HAIRY");
  194.   fparser= tryopen(filename ? filename : PFILE1, "r");
  195.  
  196.                 /* JF change from inline attrs file to separate one */
  197.   ftmp = tryopen(attrsfile, "w");
  198.   rewind(fattrs);
  199.   while((c=getc(fattrs))!=EOF)  /* Thank god for buffering */
  200.     putc(c,ftmp);
  201.   fclose(fattrs);
  202.   fattrs=ftmp;
  203.  
  204.   fguard = tryopen(guardfile, "w");
  205.  
  206. }
  207.  
  208.         /* JF to make file opening easier.  This func tries to open file
  209.            NAME with mode MODE, and prints an error message if it fails. */
  210. FILE *
  211. tryopen(name, mode)
  212. char *name;
  213. char *mode;
  214. {
  215.   FILE  *ptr;
  216.  
  217.   ptr = fopen(name, mode);
  218.   if (ptr == NULL)
  219.     {
  220.       fprintf(stderr, "bison: ");
  221.       perror(name);
  222.       done(2);
  223.     }
  224.   return ptr;
  225. }
  226.  
  227. done(k)
  228. int k;
  229. {
  230.   if (faction)
  231.     fclose(faction);
  232.  
  233.   if (fattrs)
  234.     fclose(fattrs);
  235.  
  236.   if (fguard)
  237.     fclose(fguard);
  238.  
  239.   if (finput)
  240.     fclose(finput);
  241.  
  242.   if (fparser)
  243.     fclose(fparser);
  244.  
  245. /* JF we don't need this anymore
  246.   if (fparser1)
  247.     fclose(fparser1); */
  248.  
  249.   if (foutput)
  250.     fclose(foutput);
  251.  
  252.         /* JF write out the output file */
  253.   if (k == 0 && ftable)
  254.     {
  255.       FILE *ftmp;
  256.       register int c;
  257.  
  258.       ftmp=tryopen(tabfile, "w");
  259.       rewind(ftable);
  260.       while((c=getc(ftable)) != EOF)
  261.         putc(c,ftmp);
  262.       fclose(ftmp);
  263.       fclose(ftable);
  264.     }
  265.  
  266. #ifdef VMS
  267.   delete(actfile);
  268.   delete(tmpattrsfile);
  269.   delete(tmptabfile);
  270.   if (k==0) sys$exit(SS$_NORMAL);
  271.   sys$exit(SS$_ABORT);
  272. #else
  273.   exit(k);
  274. #endif
  275. }
  276.